package common; 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

public class PageListTagNew extends TagSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int currPage  = -1;
	int total     = -1;
	int onePage   = 10;
	int oneGroup  = 10;

	public void setCurrPage(String currPage)  { 
		this.currPage  = Integer.parseInt(currPage); 
	}
	public void setCurrPage(int currPage)  { 
		this.currPage  = currPage; 
	}	
	public void setTotal(String total){ 
		this.total = Integer.parseInt(total); 
	}
	public void setTotal(int total){
		this.total = total;
	}	
	public void setOnePage(String onePage){ 
		this.onePage = Integer.parseInt(onePage); 
	}
	public void setOnePage(int onePage){ 
		this.onePage = onePage; 
	}
	public void setOneGroup(String oneGroup){ 
		this.oneGroup = Integer.parseInt(oneGroup); 
	}
	public void setOneGroup(int oneGroup){ 
		this.oneGroup = oneGroup; 
	}
	
	
	public int doStartTag(){
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		String tmp = null;
		if( currPage == -1 ){
			tmp = request.getParameter("curr_page");
			if( tmp != null && !tmp.equals("") )
				currPage = Integer.parseInt(tmp);
		}
		if( total == -1 ){
			tmp = request.getParameter("total");
			if( tmp != null && !tmp.equals("") )
				total = Integer.parseInt(tmp);
		}
		return SKIP_BODY;
	}	
	
	public int doEndTag() throws JspException{
		try{
			if( total == -1 || total == 0 )
				return EVAL_PAGE;
		
			int pages = total/onePage;
			if( total % onePage > 0 )
				pages ++;
			
			/* 뿌릴 페이지
			            currentNum  startPage  endPage
				1~10  : 0            1          11
				10~20 : 1            11         21
			*/
			int current = (int)Math.floor((currPage-1)/oneGroup);
			int last    = (int)Math.floor((pages-1)/oneGroup);
			
			int startPage = (int)(current*oneGroup)+1;	
			int endPage   = startPage + oneGroup;
			
			System.out.println("current " + current);
			System.out.println("last " + last);
			System.out.println("startPage " + startPage);
			System.out.println("endPage " + endPage);
			System.out.println("total " + total);
			System.out.println("pages " + pages);
			
			JspWriter out = pageContext.getOut();
			
			
			
			
			if(currPage!=1){
				if(current==0){
					out.print("<a href=\"javascript:go_page("+ (startPage) +")\")><img src=\"/img/bt_pprev.gif\" alt=\"이전\" /></a>&nbsp;");
				}else{
					out.print("<a href=\"javascript:go_page("+ (startPage-1) +")\")><img src=\"/img/bt_pprev.gif\" alt=\"이전\" /></a>&nbsp;");
				}
				out.print("<a href=\"javascript:go_page("+ (currPage-1) +")\")><img src=\"/img/bt_prev.gif\" alt=\"이전\" /></a>");
			}else{
				if(current==0){
					out.print("<a href=\"javascript:go_page("+ (startPage) +")\")><img src=\"/img/bt_pprev.gif\" alt=\"이전\" /></a>&nbsp;");
				}else{
					out.print("<a href=\"javascript:go_page("+ (startPage-1) +")\")><img src=\"/img/bt_pprev.gif\" alt=\"이전\" /></a>&nbsp;");
				}
				out.print("<a href=\"javascript:go_page("+ 1 +")\")><img src=\"/img/bt_prev.gif\" alt=\"이전\" /></a>");
			}
			for(int i=startPage ; i<endPage; i++){	
				if( i == currPage ){ 
					out.print("<a href=\"#\" class=\"on\">"+ i+"</span>");
				} else {	
					out.print("<a href=\"javascript:go_page("+ i +")\">"+ i +"</a>");
				}
				if( i == pages ) 
					break;
			}
			if(currPage!=endPage){
				if(currPage==pages){
					out.println("&nbsp;<a href=\"javascript:go_page("+ (pages) +")\"><img src=\"/img/bt_next.gif\" alt=\"다음\" /></a>");
				}else{
					out.println("&nbsp;<a href=\"javascript:go_page("+ (currPage+1) +")\"><img src=\"/img/bt_next.gif\" alt=\"다음\" /></a>");
				}
				if(pages>endPage){
					out.println("<a href=\"javascript:go_page("+ (endPage) +")\"><img src=\"/img/bt_nnext.gif\" alt=\"다음\" /></a>");
				}else{
					out.println("<a href=\"javascript:go_page("+ (pages) +")\"><img src=\"/img/bt_nnext.gif\" alt=\"다음\" /></a>");
				}
			}else{
				out.println("&nbsp;<a href=\"javascript:go_page("+ last +")\"><img src=\"/img/bt_next.gif\" alt=\"다음\" /></a>");
			}
			
			
		} catch (IOException e) {
			throw new JspTagException("I/O 예외 "+ e.getMessage());
		} catch (NumberFormatException e) {
			throw new JspTagException("currPage, total 값 오류 : "+ e.getMessage());
		}		
		return EVAL_PAGE;		
	}

	public void release(){
		super.release();
		currPage = -1;
		total    = -1;
		onePage  = 10;	
		oneGroup = 10;	
	}
}
